package asia.doing.controller.manage;

import asia.doing.common.Result;
import asia.doing.entity.User;
import asia.doing.service.manage.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "用户管理" ,description = "后台管理系统用户相关接口")
@RestController(value = "manageUserController")
@RequestMapping("/v1/manage/user")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "id查询用户")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user = new User();
        user.setId(id);
        List<User> users = userService.selectUser(user);
        return Result.success(users.get(0));
    }


    @ApiOperation(value = "添加用户")
    @PostMapping
    public Result addArticle(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }

    @ApiOperation(value = "更新用户")
    @PutMapping
    public Result updateArticle(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.success();
    }


    /**
     * 分页查询
     */
    @ApiOperation(value = "用户分页")
    @GetMapping
    public Result selectPage(User user,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "2") Integer pageSize) {
        PageInfo<User> page = userService.selectPage(user, pageNum, pageSize);
        return Result.success(page);
    }
}
